import selenium.webdriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest,time
from selenium.webdriver.common.keys import Keys
import GreaterWMSyaml


def chrome_driver():
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.maximize_window()
    return driver
class GreaterWMSInstellTest(unittest.TestCase):
    cfg = GreaterWMSyaml.ReadYaml.read_yaml()
    def __init__(self, driver, url=cfg['url']):
        self.driver = driver
        self.wait = WebDriverWait(driver=self.driver, timeout=5, poll_frequency=1.5)
        self.driver.get(url)

    def loc_wait(self, loc):
        return self.wait.until(EC.presence_of_element_located(loc))

    def loc_close(self):
        #按ESC取消业已打开的注册页面
        self.find_ele((By.XPATH,'/html/body/div[3]/div[2]/div/div[1]/button/span[2]/span/i')).click()

    def find_ele(self, loc):
        self.loc_wait(loc)
        return self.driver.find_element(*(loc))

    def find_eles(self, loc):
        self.loc_wait(loc)
        return self.driver.find_elements(*(loc))

    def set_language(self):
        self.find_ele((By.XPATH, '//i[text()="translate"]/ancestor::button')).click()
        self.find_ele((By.XPATH, '//div[text()="中文简体"]/parent::div')).click()
        time.sleep(5)

    def login(self, username='1', passwrod='1'):
        self.find_ele((By.XPATH, '//span[text()="登入"]/ancestor::button')).click()
        #self.find_ele((By.XPATH, '//span[text()="管理员登入"]/ancestor::button')).click()
        self.find_ele((By.XPATH,'/html/body/div[3]/div[2]/div/div[1]/div[1]/div/div[2]/div[2]')).click()
        self.find_ele((By.XPATH, '//input[@aria-label="管理员"]')).send_keys(username)
        self.find_ele((By.XPATH, '//input[@aria-label="密码"]')).send_keys(passwrod)
        self.find_ele((By.XPATH, '//div[contains(@class,"justify-start")]//'
                                 'span[text()="登入"]/ancestor::button')).click()
        time.sleep(1)

    def add_goods_place(self, place=cfg['goods']['place']):
        self.find_ele((By.XPATH, '//div[contains(text(),"商品管理")]/parent::a')).click()
        self.find_ele((By.XPATH,'//div[text()="商品产地"]/ancestor::a')).click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'//span[@class="block" and text()="新增"]').click()
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]'
                                          '/div/div[2]/label/div/div[1]/div/input').send_keys(place)
        self.find_ele((By.XPATH, '//span[text()="确认"]/ancestor::button')).click()
        time.sleep(5)

    def add_goods_specification(self,specname=cfg['goods']['spechficationname']):
        #self.find_ele((By.XPATH, '//div[contains(text(),"商品管理")]/parent::a')).click()
        self.find_ele((By.XPATH,'//div[text()="商品规格"]/ancestor::a')).click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'//span[@class="block" and text()="新增"]').click()
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]'
                                          '/div/div[2]/label/div'
                                          '/div[1]/div[1]/input').send_keys(specname)
        self.find_ele((By.XPATH, '//span[text()="确认"]/ancestor::button')).click()
    def add_goods_shape(self,shapename=cfg['goods']['shapename']):
        #self.find_ele((By.XPATH, '//div[contains(text(),"商品管理")]/parent::a')).click()
        self.find_ele((By.XPATH,'//div[text()="商品形状"]/ancestor::a')).click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'//span[@class="block" and text()="新增"]').click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]'
                                          '/div/div[2]/label/div'
                                          '/div[1]/div[1]/input').send_keys(shapename)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[3]'
                                          '/button[2]/span[2]/span').click()
    def add_goods_brand(self,brandname=cfg['goods']['brandname']):
        #self.find_ele((By.XPATH, '//div[contains(text(),"商品管理")]/parent::a')).click()
        self.find_ele((By.XPATH,'//div[text()="商品品牌"]/ancestor::a')).click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'//span[@class="block" and text()="新增"]').click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]'
                                          '/div/div[2]/label/div'
                                          '/div[1]/div/input').send_keys(brandname)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[3]'
                                          '/button[2]/span[2]/span').click()

    def add_goods_color(self,colorname=cfg['goods']['colorname']):
        #self.find_ele((By.XPATH, '//div[contains(text(),"商品管理")]/parent::a')).click()
        self.find_ele((By.XPATH,'//div[text()="商品颜色"]/ancestor::a')).click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'//span[@class="block" and text()="新增"]').click()
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]'
                                          '/div/div[2]/label/div'
                                          '/div[1]/div[1]/input').send_keys(colorname)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[3]'
                                          '/button[2]/span[2]/span').click()
    def add_goods_category(self,categoryname=cfg['goods']['categoryname']):
        #self.find_ele((By.XPATH, '//div[contains(text(),"商品管理")]/parent::a')).click()
        self.find_ele((By.XPATH,'//div[text()="商品类别"]/ancestor::a')).click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'//span[@class="block" and text()="新增"]').click()
        self.driver.find_element(By.XPATH,'/html/body/div[3]'
                                          '/div[2]/div/div[2]/label/div'
                                          '/div[1]/div/input').send_keys(categoryname)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[3]'
                                          '/button[2]/span[2]/span').click()

    def add_goods_unit(self,unitname=cfg['goods']['unitname']):
        #self.find_ele((By.XPATH, '//div[contains(text(),"商品管理")]/parent::a')).click()
        self.find_ele((By.XPATH,'//div[text()="商品单位"]/ancestor::a')).click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'//span[@class="block" and text()="新增"]').click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'/html/body/div[3]'
                                          '/div[2]/div/div[2]/label/div'
                                          '/div[1]/div/input').send_keys(unitname)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[3]'
                                          '/button[2]/span[2]/span').click()

    def add_goods(self,goodsname=cfg['goods']['goodsname'],
                  goodsdescribe=cfg['goods']['goodsdescribe'],
                  goodweight=cfg['goods']['goodweight'],
                  goodlenght=cfg['goods']['goodlenght'],
                  goodwidth=cfg['goods']['goodwidth'],
                  goodheight=cfg['goods']['goodheight'],
                  goodscosts=cfg['goods']['goodscosts'],
                  goodsprices=cfg['goods']['goodsprices']):
        self.find_ele((By.XPATH, '//div[contains(text(),"商品管理")]/parent::a')).click()
        self.driver.find_element(By.XPATH,'//*[@id="q-app"]/div/div[2]/main'
                                          '/div[1]/div/div/div/a[1]/div[2]/div').click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'//span[@class="block" and text()="新增"]').click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div'
                                          '/div[2]/label[1]'
                                          '/div/div[1]/div/input').send_keys(goodsname)
        time.sleep(5)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]'
                                          '/div/div[2]/label[2]'
                                          '/div/div[1]/div[1]/input').send_keys(goodsdescribe)
        time.sleep(5)
        self.driver.find_element(*(By.XPATH, '//div[text()="供应商"]/ancestor::label')).click()
        self.driver.find_element(*(By.XPATH, '//div[@class="q-item__label" '
                                         'and text()="51testing"]')).click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]'
                                          '/label[4]/div/div[1]/div/input').send_keys(goodweight)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]'
                                          '/label[5]/div/div[1]/div/input').send_keys(goodlenght)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]'
                                          '/label[6]/div/div[1]/div[1]/input').send_keys(goodwidth)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]'
                                          '/label[7]/div/div[1]/div/input').send_keys(goodheight)
        time.sleep(10)
        self.driver.find_element(*(By.XPATH, '//div[text()="商品单位"]/ancestor::label')).click()
        self.driver.find_element(*(By.XPATH, '//div[@class="q-item__label" '
                                         'and text()="盒"]')).click()
        time.sleep(10)
        self.driver.find_element(*(By.XPATH, '//div[text()="商品类别"]/ancestor::label')).click()
        self.driver.find_element(*(By.XPATH, '//div[@class="q-item__label" '
                                         'and text()="箱"]')).click()
        time.sleep(20)
        self.driver.find_element(*(By.XPATH, '//div[text()="商品品牌"]/ancestor::label')).click()
        self.driver.find_element(*(By.XPATH, '//div[@class="q-item__label" '
                                             'and text()="玉西"]')).click()
        time.sleep(20)
        self.driver.find_element(By.XPATH, '/html/body/div[3]'
                                           '/div[2]/div/div[2]/label[11]').click()
        self.driver.find_element(*(By.XPATH, '//div[@class="q-item__label" '
                                             'and text()="黄"]')).click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'/html/body/div[3]'
                                          '/div[2]/div/div[2]/label[12]').click()
        self.driver.find_element(*(By.XPATH, '//div[@class="q-item__label" '
                                             'and text()="框"]')).click()
        time.sleep(5)
        self.driver.find_element(By.XPATH,'/html/body/div[3]'
                                          '/div[2]/div/div[2]/label[13]').click()
        self.driver.find_element(*(By.XPATH, '//div[@class="q-item__label" '
                                             'and text()="3*350"]')).click()
        self.driver.find_element(By.XPATH,'/html/body/div[3]'
                                          '/div[2]/div/div[2]/label[14]').click()
        self.driver.find_element(*(By.XPATH, '//div[@class="q-item__label" '
                                             'and text()="广州"]')).click()
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]/label[15]'
                                          '/div/div[1]/div/input').send_keys(goodscosts)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]/label[16]'
                                          '/div/div[1]/div/input').send_keys(goodsprices)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[3]'
                                          '/button[2]/span[2]/span').click()
class TestInitGreaterWMS(unittest.TestCase):

    def setUp(self):
        self.driver = chrome_driver()

    def test_01(self):
        page = GreaterWMSInstellTest(self.driver)
        page.loc_close()
        page.set_language()
        page.loc_close()
        page.login()
        page.add_goods_place()
        page.add_goods_specification()
        page.add_goods_shape()
        page.add_goods_brand()
        page.add_goods_color()
        page.add_goods_category()
        page.add_goods_unit()
        #page.add_goods()
        time.sleep(5)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()